package com.jsp.controller.usercontroller;

import com.jsp.entity.UserVO;
import com.jsp.entity.Bzdm;
import com.jsp.entity.Depart;
import com.jsp.entity.User;
import com.jsp.mapper.impl.BzdmMapperImpl;
import com.jsp.mapper.impl.DepartMapperImpl;
import com.jsp.mapper.impl.UserMapperImpl;
import com.jsp.util.MD5Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@WebServlet(name = "UserLoginServlet", value = "/userLogin")
public class UserLoginServlet extends HttpServlet {
//登录操作
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        try{
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String remember = request.getParameter("remember");
        User user = new User();
        user.setYhid(username);
        user.setYhkl(password);
        boolean check = checkUserNamePassword(user);

        if (check) {
            if("1".equals(remember)){
                Cookie uname = new Cookie("username", username);
                Cookie pwd = new Cookie("password", password);
                response.addCookie(uname);
                response.addCookie(pwd);
            }

//            List<UserVO> collect = getUserVOS();
//            request.setAttribute("userList", collect);
//
//            DepartMapperImpl departMapper = new DepartMapperImpl();
//            request.setAttribute("departList", departMapper.findAll());

            response.sendRedirect("/jsp_demo/list.jsp");
        } else {
            request.setAttribute("errMessage", "用户名或密码错误");
            request.getRequestDispatcher("/index.jsp").forward(request, response);
        }
        }catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static List<UserVO> getUserVOS() {
        BzdmMapperImpl bzdmMapper = new BzdmMapperImpl();
        Map<String, Bzdm> sexs = bzdmMapper.findAll().stream()
                .collect(Collectors.toMap(k -> k.getCode(), v -> v));
        DepartMapperImpl departMapper = new DepartMapperImpl();
        Map<String, Depart> departs = departMapper.findAll().stream()
                .collect(Collectors.toMap(k -> k.getBmdm(), v -> v));

        UserMapperImpl userMapper = new UserMapperImpl();
        List<User> all = userMapper.findAll();
        List<UserVO> collect = all.stream().map(user -> {
            UserVO userVO = new UserVO();
            userVO.setYhxm(user.getYhxm());
            userVO.setYhid(user.getYhid());
            userVO.setSex(sexs.get(user.getYhxb()).getMc());
            userVO.setDepart(departs.get(user.getYhbm()).getBmmc());
            return userVO;
        }).collect(Collectors.toList());
        return collect;
    }

    private boolean checkUserNamePassword(User user) {
        UserMapperImpl userMapper = new UserMapperImpl();
        User admin = userMapper.findByUsername(user.getYhid());
//        if (!Objects.isNull(admin) && admin.getYhkl().equals(user.getYhkl())) {
        if (!Objects.isNull(admin) && admin.getYhkl().equals(MD5Utils.getEncode(user.getYhkl(),user.getYhid()))) {
            return true;
        }
        return false;
    }

}
